package com.dong.mcd.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dong.mcd.dao.SysRoleMapper;
import com.dong.mcd.model.SysRole;
import com.dong.mcd.model.SysRoleExample;
import com.dong.mcd.model.SysRoleExample.Criteria;
import com.dong.mcd.param.RoleParam;
import com.dong.mcd.service.IRoleService;
import com.dong.mcd.util.PageConfig;

@Transactional
@Service("roleService")
public class RoleServiceImpl implements IRoleService {

	@Autowired
	private SysRoleMapper roleMapper;

	public void add(SysRole role) {
		roleMapper.insert(role);
	}

	public SysRole get(Integer id) {
		return roleMapper.selectByPrimaryKey(id);
	}

	public void modify(SysRole role) {
		roleMapper.updateByPrimaryKeySelective(role);
	}

	public List<SysRole> query(RoleParam param) {
		SysRoleExample example = new SysRoleExample();
		Criteria criteria = example.createCriteria();
		if (StringUtils.isNotEmpty(param.getRoleName()))
			criteria.andRoleNameEqualTo(param.getRoleName());
		if (param.getStatus() != null)
			criteria.andStatusEqualTo(param.getStatus());
		return roleMapper.selectByExample(example);
	}

	public PageConfig queryPage(RoleParam param, Integer pageIndex,
			Integer pageSize) {
		SysRoleExample example = new SysRoleExample();
		Criteria criteria = example.createCriteria();
		if (StringUtils.isNotEmpty(param.getRoleName()))
			criteria.andRoleNameLike("%"+param.getRoleName()+"%");
		if (param.getStatus() != null)
			criteria.andStatusEqualTo(param.getStatus());
		return new PageConfig(roleMapper,example,pageIndex,pageSize);
	}

	public void remove(Integer id) {
		roleMapper.deleteByPrimaryKey(id);
	}

	public void grantMenus(RoleParam param) {
		roleMapper.deleteRoleMenu(param.getId());
		roleMapper.saveRoleMenu(param);
	}

}
